
#include "stack.h"
#include <stdlib.h>
#include <stdio.h>

void stack::init(number maxSize)
{
	array = (stackElem*)malloc(maxSize * sizeof(stackElem));
	if(!array) texp("neni pamet");
	size = maxSize;
	top = 0;
}

void stack::cleanup()
{
	free(array);
	array = NULL;
}

stackElem stack::pop()
{
	if(top == 0) texp("odebirani z prazdneho zasobniku");
	else { top--; return array[top]; }
}

void stack::push(stackElem num)
{
	if(top >= size) texp("pridani do plneho zasobniku");
	else array[top++] = num;
}

stackElem stack::pick(number depth)
{
	if(depth >= top) texp("cteni mimo zasobnik");
	else return array[top];
}

void stack::print(number count)
{
	number printSize;
	number p;

	if(count == 0) printSize = top;
	else
	{
		if(top < count) texp("zasobnik neni tak velky");
		printSize = count;
	}

	for(p = 0; p < printSize; p++)
		printf("%u\n", array[top - p - 1]);
}

bool stack::isEmpty()
{
	return top == 0;
}
